#include "FrameWnd.h"

CFrameWnd::CFrameWnd()
{
}

CFrameWnd::~CFrameWnd()
{
}

LRESULT CFrameWnd::OnCreate(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
   RECT rcPos = {0, 0, 0, 0};
   bHandled = TRUE;
   
   if (!m_reBar.Create(m_hWnd, rcPos, REBAR_ID)) {
      return -1;
   }
   if (!m_statusBar.Create(m_hWnd, STATUSBAR_ID)) {
      return -1;
   }
   
   m_reBar.SetFrame(this);
   return 0;
}

LRESULT CFrameWnd::OnClose(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
   DestroyWindow();
   return 0;
}

LRESULT CFrameWnd::OnNcDestroy(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandle)
{
   PostQuitMessage(0);
   return 0;
}

LRESULT CFrameWnd::OnSize(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
   bHandled = FALSE;
   if (wParam == SIZE_MINIMIZED) {
      return 0;
   }
   
   WORD wWidth = LOWORD(lParam);
   WORD wHeight = HIWORD (lParam);
   
   RECT rc;
   ::GetWindowRect(m_reBar.m_hWnd, &rc);
   ScreenToClient(&rc);
   rc.left = 0;
   rc.right = wWidth;
   m_reBar.MoveWindow(&rc, TRUE);
   
   ::GetWindowRect(m_statusBar.m_hWnd, &rc);
   ScreenToClient(&rc);
   UINT uSBHeight = rc.bottom - rc.top;
   rc.bottom = wHeight;
   rc.top = rc.bottom - uSBHeight; 
   rc.left = 0;
   rc.right = wWidth;
   m_statusBar.MoveWindow(&rc, TRUE);

   CalcLayout();
   return 0;
}

LRESULT CFrameWnd::OnMenuSelect(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
   HMENU hMenu = GetMenu(m_hWnd);
   m_statusBar.MenuHelp(msg, wParam, lParam, hMenu);
   return 0;
}
   
void CFrameWnd::CalcLayout(LPRECT lpRect)
{
   assert (::IsWindow(m_hWnd));

   RECT rcContent, rcRebar, rcStatusBar;
   GetClientRect(&rcContent);
   if (::IsWindow(m_reBar.m_hWnd)) {
      m_reBar.GetWindowRect(&rcRebar);
      ScreenToClient(&rcRebar);
      rcContent.top = rcRebar.bottom;
   }
   if (::IsWindow(m_statusBar.m_hWnd)) {
      m_statusBar.GetWindowRect(&rcStatusBar);
      ScreenToClient(&rcStatusBar);
      rcContent.bottom = rcStatusBar.top;
   }
   
   if (lpRect) {
      *lpRect = rcContent;
   }
   if (::IsWindow(m_hwndContent)) {
      ::MoveWindow(m_hwndContent, rcContent.left, rcContent.top, 
                  rcContent.right - rcContent.left ,
                  rcContent.bottom - rcContent.top, TRUE);
   } 
   return;
}

LRESULT CFrameWnd::OnRebarSize(int idCtrl, LPNMHDR lpNmhdr, BOOL& bHandled)
{
   CalcLayout();
   return 0;
}

LRESULT CFrameWnd::OnToolTipInfo(int idCtrl, LPNMHDR lpNmhdr, BOOL& bHandled)
{
   LPNMTTDISPINFO lpInfo = (LPNMTTDISPINFO)lpNmhdr;

   if (lpInfo->uFlags == TTF_IDISHWND) {
      return 0;
   }

   lpInfo->lpszText = MAKEINTRESOURCE(idCtrl);
   lpInfo->hinst = _Module.GetModuleInstance();
   bHandled = TRUE;
   return 0;
}
